package com.itheima.my_reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.my_reggie.common.CustomException;
import com.itheima.my_reggie.dao.CategoryDao;
import com.itheima.my_reggie.pojo.Category;
import com.itheima.my_reggie.pojo.Dish;
import com.itheima.my_reggie.pojo.Setmeal;
import com.itheima.my_reggie.service.ICategoryService;
import com.itheima.my_reggie.service.IDishService;
import com.itheima.my_reggie.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CategoryServiceImpl
        extends ServiceImpl<CategoryDao, Category>
        implements ICategoryService {
//  注入需要的数据信息
    @Autowired
    private IDishService dishService;
    //  注入需要的数据信息
    @Autowired
    private ISetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishWrapper);
        if(dishCount>0){
            throw  new CustomException("当前分类下已关联了菜品，无法删除");
        }
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId,id);
        int setmealCount = setmealService.count(setmealWrapper);
        if(setmealCount>0){
            throw  new CustomException("当前套餐下已关联了菜品，无法删除");
        }
        log.info("dishCount={}",dishCount);
        log.info("setmealCount={}",setmealCount);
        super.removeById(id);
    }
}
